package com.zhiche.lisa.oms.service.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UaaUtil {

    @Value("${uaa.getTentUrl}")
    private String getTentUrl;

    /**
     * 得到租户应用管理员
     * @return
     */
    public List<String> getToken(){
        List<String> techAccountIdentifiers = Lists.newArrayList();
        String strReturn = HttpClientUtil.get(getTentUrl, null, 10000);
        JSONObject jsonObject = JSON.parseObject(strReturn);
        int status = jsonObject.getInteger("code");
        if(status == 0){
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            if(CollectionUtils.isNotEmpty(jsonArray)){
                jsonArray.forEach(object1 -> {
                    JSONObject jsonObject1 = (JSONObject) object1;
                    String techAccountIdentifier = jsonObject1.getString("techAccountIdentifier");
                    techAccountIdentifiers.add(techAccountIdentifier);
                });
            }
        }
        return techAccountIdentifiers;
    }
}
